using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Main.controls;
using Main;
namespace Main.polls
{
    public partial class show : System.Web.UI.Page
    {        
        protected void Page_Load(object sender, EventArgs e)
        {
            Poll currentPoll;

            currentPoll = new Poll();
            int test = Convert.ToInt32(Request.QueryString["id"]);
            currentPoll.FillByID(test);
            pollTitle.Text = currentPoll.Title;
            pollQuestion.Text = currentPoll.Question;
            pollSummary.Text = currentPoll.Summary;
            pollSummary.Text += currentPoll.Summary;
            pollSummary.Text += currentPoll.Summary;
            
            //Next, let's try check to see if they are authenticated
            //If so, let them add a comment.
            if (this.User.Identity.IsAuthenticated)
            {
                CreateComment newComment = (CreateComment)LoadControl("/controls/CreateComment.ascx");
                this.pollcommentform.Controls.Add(newComment);
            }

            CommentCollection pollComments = new CommentCollection();
            ShowComment myComment;
            pollComments.GetCommentsForPoll(currentPoll.ID);
            foreach (Comment com in pollComments)
            {
                myComment = (ShowComment)LoadControl("/controls/ShowComment.ascx");
                myComment.CommentID = com.ID;
                myComment.CurrentComment = com;
                this.pollcomments.Controls.Add(myComment);
            }
            
            if (Request.RequestType == "POST")
            {
                int optionid = Convert.ToInt32(Request["option"]);
                try
                {
                    DataAccess.CreateVote((Guid)Membership.GetUser().ProviderUserKey, currentPoll.ID, optionid);
                }
                catch (Exception)
                {
                    error.Visible = true;
                    error.Text = "You have already voted on this poll, your vote was not counted.";
                    btnVote.Visible = false;

                    string data = "";
                    string options = "";
                    Random rand = new Random();
                    foreach (Option option in currentPoll.Options)
                    {
                        option.TotalVotes = rand.Next(0, 100);

                        pollOptions.InnerHtml += "<div class='pollOption'>";
                        pollOptions.InnerHtml += "<label for='" + option.OptionID.ToString() + "'>" + option.PollOption + ": \t" + option.TotalVotes + "</label></div>";
                        
                        data += option.TotalVotes + ",";
                        options += option.PollOption + "|";
                    }
                    data = data.Substring(0, data.Length - 1);
                    options = options.Substring(0, options.Length - 1);

                    chart.Src = "http://chart.apis.google.com/chart?cht=p3&chd=t:" + data + "&chs=300x100&chl=" + options;
                }
            }
            else
            {
                foreach (Option option in currentPoll.Options)
                {
                    pollOptions.InnerHtml += "<div class='pollOption'>";
                    pollOptions.InnerHtml += "<input id='" + option.OptionID.ToString() +
                                             "' value='" + option.OptionID + "' type='radio' name='options' />";

                    pollOptions.InnerHtml += "<label for='" + option.OptionID.ToString() + "'>" + option.PollOption + "</label></div>";
                }
            }
        }   
    }
}
